package cn.edu.scau.dormitory_management_system.controller;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.dormitory_management_system.entity.BillEntity;
import cn.edu.scau.dormitory_management_system.entity.DormEntity;
import cn.edu.scau.dormitory_management_system.entity.DormbuildEntity;
import cn.edu.scau.dormitory_management_system.entity.StudentEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.StuDTO;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.DormbuildService;
import cn.edu.scau.dormitory_management_system.service.StudentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/dormbuild")
public class DormbuildController {



    @Autowired
    DormbuildService dormbuildService;

    @Autowired
    DormService dormService;

    @Autowired
    BedService bedService;

    @Autowired
    StudentService studentService;

    @PostMapping("getAllBuilds")
    public JSONObject getAllBuild(@RequestParam(defaultValue = "1") int current,
               @RequestParam(defaultValue = "8") int size) {
        JSONObject object = new JSONObject();
        /*
        List<DormbuildEntity> dormbuildEntities = dormbuildService.getAllBuild();
        object.put("builds", dormbuildEntities);
        */
        IPage<DormbuildEntity> page = new Page<>(current,size);
        QueryWrapper<DormbuildEntity> wrapper = new QueryWrapper<>();
        page = dormbuildService.page(page);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("/test")
    public void test(@RequestParam("num") int num){
        for(int i = num;i<=num+30;i++){
            StudentEntity studentEntity = new StudentEntity(23,String.valueOf(i),"无",0,"12345678900","工程学院","土木工程",20,"1班");
            studentService.save(studentEntity);
        }
    }
    @PostMapping("/getAllIds")
    public JSONObject getAllIds() {
        JSONObject object = new JSONObject();

        List<Integer> ids = new ArrayList<>();
        for (DormbuildEntity dormbuildEntity : dormbuildService.list()) {
            ids.add(dormbuildEntity.getBuildId());
        }

        if(ids.isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("ids", ids);
        }

        return object;
    }


    @PostMapping("create")
    public JSONObject create(@RequestBody DormbuildEntity dormbuildEntity){
        boolean flag = dormbuildService.create(dormbuildEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
            BillEntity billEntity =new BillEntity();
        }
        return object;
    }


    @PostMapping("delete")
    public JSONObject delete(@RequestBody DormbuildEntity dormbuildEntity){
        boolean flag = dormbuildService.delete(dormbuildEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("update")
    public JSONObject update(@RequestBody DormbuildEntity dormbuildEntity){
        boolean flag = dormbuildService.update(dormbuildEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("getAllStudent")
    public JSONObject getAllStudent(@RequestParam("buildId") int buildId,
                                    @RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "8") int size){
       /** List<DormEntity> dormEntities = dormService.findDormByBuildId(buildId);
        List<StuDTO> stuDTOS = new ArrayList<>();
        for (DormEntity dormEntity:dormEntities
             ) {
            stuDTOS.addAll(dormService.getStudentDto(dormEntity));
        }
        boolean flag = stuDTOS.isEmpty(); **/
        IPage<StuDTO> page = new Page<>(current,size);
        page=dormbuildService.getStuDTO(page,buildId);
        JSONObject object = new JSONObject();
        if(!page.getRecords().isEmpty()){
            object.put("studentDTO", page);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "无数据");
        }
        return object;
    }

    @PostMapping("findBuildByName")
    public JSONObject findBuildByName(@RequestParam("buildName") String buildName){
        JSONObject object = new JSONObject();
        List<DormbuildEntity> dormbuildEntities = dormbuildService.findBuildByName(buildName);
        object.put("builds", dormbuildEntities);
        if(dormbuildEntities.isEmpty()){
            object.put("code",0);
            object.put("msg","无此楼栋");
        }
        else {
            object.put("code",200);
        }
        return object;
    }


}
